package com.bj58.ecdata.dashboard.databll;

import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

import com.alibaba.fastjson.JSONObject;
import com.bj58.ecdata.dao.util.Common;
import com.bj58.ecdata.dashboard.cache.ServiceBeanFactory;
import com.bj58.ecdata.dashboard.constant.EnumCate;
import com.bj58.ecdata.dashboard.dbentity.CalendarCity;
import com.bj58.ecdata.dashboard.dbentity.CalendarSource;
import com.bj58.ecdata.dashboard.dbentity.InfoCore;
import com.bj58.ecdata.dashboard.service.AbstractBaseService;
import com.bj58.ecdata.dashboard.utils.ReflectUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class CalendarBLL {

	private static AbstractBaseService<InfoCore> infoService =ServiceBeanFactory.get(InfoCore.class,null);
	private static AbstractBaseService<CalendarSource> calendarSourceService = ServiceBeanFactory.get(CalendarSource.class, null);
	private static AbstractBaseService<CalendarCity> calendarCityService = ServiceBeanFactory.get(CalendarCity.class, null);

	/**
	 * 万年历节日城市数据--折线图
	 * @param bus 
	 * @param city 
	 * @param cate
	 * @param index
	 * @param festival
	 * @param dateGroup
	 * @param platform
	 * @return
	 */
	public static String getCalendarFesLine(EnumCate bus,String dateGroup,String cate,String city,String index,String festival,String platform) {
		
		
		//获取节日对应的各年的日期 和当天所查指标的值
		List<String> festivalList = Lists.newArrayList();
		List<String> legend = Lists.newArrayList();
		HashMap<String,Integer> festivalMap = Maps.newHashMap();
		
		//替换业务线的总体名称			
		String bus_sql="";
		if(bus.toString().equals("total")){
			bus_sql="all";
		}else
		{
			bus_sql=bus.toString();
		}
		
		//表后缀  二手创新和二手推广还在二手的表里。。
		String tableSuffix="";
		if(bus.toString().equals("total"))
			tableSuffix="all";
		else if(bus.toString().equals("ershouchuangxin") || bus.toString().equals("ershoutuiguang"))
		{
			tableSuffix="ershou";
		}else
		{
			tableSuffix=bus.toString();
		}
			
		String stat_type="";
		if(dateGroup.equals("stat_date"))
			stat_type="day";
		else if(dateGroup.equals("week_begin"))
			stat_type="week";
		
		String sel =" distinct "+dateGroup+","+index+" as uv";
		String con = "business_type='"+bus_sql+"' AND disp_city1_name='"+city+"' AND disp_cate2_name='"+cate+"' AND stat_type='"+stat_type+"' AND platform='"+platform+"'  AND holiday= '"+festival+"' group by "+dateGroup;				
		List<CalendarCity> yearlist = calendarCityService.getListByConditon(tableSuffix,sel, con, dateGroup);
		
		for(CalendarCity v:yearlist)
		{	
			//如果某一年节日当天的值为0，就不加入各种list显示了
			if(v.getUv()==0)
			{
				continue;
			}
			
			festivalList.add(ReflectUtil.getVal(v, dateGroup).toString());
			festivalMap.put(ReflectUtil.getVal(v, dateGroup).toString(),v.getUv());
			legend.add(ReflectUtil.getVal(v, dateGroup).toString().substring(0, 4));
		} 
		List<Object> days = Lists.newArrayList();
		JSONObject ob = new JSONObject();
			
		if(festivalList.isEmpty())
		{
			return ob.toString();
		}
		
		//分别获取各年对应节日的前后4周数据
		String select1 = dateGroup+","+index+" as uv, '"+festivalList.get(0)+"' as flag";
		String condition1 ="business_type='"+bus_sql+"' AND disp_city1_name='"+city+"' AND disp_cate2_name='"+cate+"' AND stat_type='"+stat_type+"' AND platform='"+platform+"' " +
		"AND "+dateGroup+">= date_sub('"+festivalList.get(0)+"',interval 31 day) AND "+dateGroup+"<= date_sub('"+festivalList.get(0)+"',interval -31 day) group by "+dateGroup;	
		for (String fes : festivalList){
			if(fes.equals(festivalList.get(0)))
				continue;

				String c=" union select "+dateGroup+","+index+" as uv, '"+fes+"' as flag from "+Common.getTableName(CalendarCity.class)+bus_sql+" where business_type='"+bus_sql+"' AND disp_city1_name='"+city+"' AND disp_cate2_name='"+cate+"' AND stat_type='"+stat_type+"' AND platform='"+platform+"' " +
				"AND "+dateGroup+">= date_sub('"+fes+"',interval 31 day) AND "+dateGroup+"<= date_sub('"+fes+"',interval -31 day) group by "+dateGroup;
				condition1=condition1+c;		
		}		
		
		List<CalendarCity> list1 = calendarCityService.getListByConditon(tableSuffix,select1, condition1, dateGroup);
		//stat_dateMap对应数据   节日当天=[...]
		TreeMap<String, List<CalendarCity>> stat_dateMap = new TreeMap<String, List<CalendarCity>>(ReflectUtil.groupByField(list1, "flag"));
		
		JSONObject truedatajson = new JSONObject();   //真实值
		JSONObject difdatajson = new JSONObject();    //与节日当天的差值
		JSONObject trueTimejson = new JSONObject();   //对应的时间
		
		for (String fes : stat_dateMap.keySet()){
			
			List<Object> CountList = Lists.newArrayList();  //归一化数据
			List<Object> turedatalist=Lists.newArrayList();
			List<Object> difdatalist=Lists.newArrayList();
			List<Object> turetimelist=Lists.newArrayList();
			
			List<CalendarCity> list=stat_dateMap.get(fes);
	    	for (CalendarCity vo : list) {
	    		
	    		if(festivalList.size()==1 ||  fes.equals(festivalList.get(festivalList.size()-2)))//时间轴按去年显示
	    		{
	    			days.add(ReflectUtil.getVal(vo, dateGroup));
	    		}
	
	    		//展示的是所有数据除以节日当天
	    		double curdate_data= Integer.parseInt(ReflectUtil.getVal(vo, "uv").toString());
	    		double fes_data= festivalMap.get(fes);
	    		double t=(double)(Math.round(curdate_data/fes_data*10000)/10000.0);
				CountList.add(t); 
				
				turedatalist.add(curdate_data);
				difdatalist.add(curdate_data-fes_data);
				turetimelist.add(ReflectUtil.getVal(vo,dateGroup));    
			} 	
		    
	    	truedatajson.put(fes.substring(0, 4), turedatalist);
	    	difdatajson.put(fes.substring(0, 4), difdatalist);    	
		    ob.put(fes.substring(0, 4), CountList);		
			trueTimejson.put(fes.substring(0, 4), turetimelist);			
		  
		}		
		
		ob.put("time", days);
		ob.put("legend", legend);		
		ob.put("truedatajson", truedatajson);
		ob.put("difdatajson", difdatajson);	
		ob.put("trueTimejson", trueTimejson);			
		return ob.toString();
	}
	
	
	/**
	 * 万年历节日来源数据--折线图
	 * @param bus 
	 * @param cate 
	 * @param source
	 * @param index
	 * @param festival
	 * @param dateGroup
	 * @param platform
	 * @return
	 */
	
	public static String getCalSourceLine(EnumCate bus,String dateGroup,String cate,String source,String index,String festival,String platform) {
		
		//获取节日对应的各年的日期
		List<String> festivalList = Lists.newArrayList();
		List<String> legend = Lists.newArrayList();
		HashMap<String,Integer> festivalMap = Maps.newHashMap();
		
		String bus_sql="";
		if(bus.toString().equals("total"))
			bus_sql="all";
		else bus_sql=bus.toString();
		
		String stat_type="";
		if(dateGroup.equals("stat_date"))
			stat_type="day";
		else if(dateGroup.equals("week_begin"))
			stat_type="week";
		
		String sel  =" distinct "+dateGroup+","+index+" as uv";
		String con = "business_type='"+bus_sql+"' AND source1_name='"+source+"' AND disp_cate2_name='"+cate+"' AND stat_type='"+stat_type+"' AND holiday= '"+festival+"' group by "+dateGroup;				
		List<CalendarSource> yearlist = calendarSourceService.getListByConditon(platform,sel, con, dateGroup);
		
		for(CalendarSource v:yearlist)
		{			
			//如果某一年节日当天的值为0，就不加入各种list显示了
			if(v.getUv()==0)
			{
				continue;
			}
			
			festivalList.add(ReflectUtil.getVal(v, dateGroup).toString());
			festivalMap.put(ReflectUtil.getVal(v, dateGroup).toString(),v.getUv());
			legend.add(ReflectUtil.getVal(v, dateGroup).toString().substring(0, 4));			
		}
		 
		List<Object> days = Lists.newArrayList();
		JSONObject ob = new JSONObject();
	
		if(festivalList.isEmpty())
		{
			return ob.toString();
		}
		
		//分别获取各年对应节日的前后4周数据
		JSONObject truedatajson = new JSONObject();   //真实值
		JSONObject difdatajson = new JSONObject();    //与节日当天的差值
		JSONObject trueTimejson = new JSONObject();   //对应的时间
		
		String select1 = dateGroup+","+index+" as uv, '"+festivalList.get(0)+"' as flag";
		String condition1 ="business_type='"+bus_sql+"' AND source1_name='"+source+"' AND disp_cate2_name='"+cate+"' AND stat_type='"+stat_type+"' " +
		"AND "+dateGroup+">= date_sub('"+festivalList.get(0)+"',interval 31 day) AND "+dateGroup+"<= date_sub('"+festivalList.get(0)+"',interval -31 day) group by "+dateGroup;	
		for (String fes : festivalList){
			if(fes.equals(festivalList.get(0)))
				continue;

				String c=" union select "+dateGroup+","+index+" as uv, '"+fes+"' as flag from "+Common.getTableName(CalendarSource.class)+platform+" where business_type='"+bus_sql+"' AND source1_name='"+source+"' AND disp_cate2_name='"+cate+"' AND stat_type='"+stat_type+"' " +
				"AND "+dateGroup+">= date_sub('"+fes+"',interval 31 day) AND "+dateGroup+"<= date_sub('"+fes+"',interval -31 day) group by "+dateGroup;
				condition1=condition1+c;		
		}	
		
		List<CalendarSource> list1 = calendarSourceService.getListByConditon(platform,select1, condition1, dateGroup);
		//stat_dateMap对应数据   节日当天=[...]
		TreeMap<String, List<CalendarSource>> stat_dateMap = new TreeMap<String, List<CalendarSource>>(ReflectUtil.groupByField(list1, "flag"));
		
		for (String fes : stat_dateMap.keySet()){
			
			List<Object> CountList = Lists.newArrayList();  //归一化数据
			List<Object> turedatalist=Lists.newArrayList();
			List<Object> difdatalist=Lists.newArrayList();
			List<Object> turetimelist=Lists.newArrayList();
			
			List<CalendarSource> list=stat_dateMap.get(fes);
	    	for (CalendarSource vo : list) {
	    		
	    		if(festivalList.size()==1 ||  fes.equals(festivalList.get(festivalList.size()-2)))//时间轴按去年显示
	    		{
	    			days.add(ReflectUtil.getVal(vo, dateGroup));
	    		}
	    		//展示的是所有数据除以节日当天
	    		double curdate_data= Integer.parseInt(ReflectUtil.getVal(vo, "uv").toString());
	    		double fes_data= festivalMap.get(fes);
	    		double t=(double)(Math.round(curdate_data/fes_data*10000)/10000.0);
				CountList.add(t); 
				
				turedatalist.add(curdate_data);
				difdatalist.add(curdate_data-fes_data);
				turetimelist.add(ReflectUtil.getVal(vo,dateGroup));    
			} 	
	    	
	    	truedatajson.put(fes.substring(0, 4), turedatalist);
	    	difdatajson.put(fes.substring(0, 4), difdatalist);    	
		    ob.put(fes.substring(0, 4), CountList);		
			trueTimejson.put(fes.substring(0, 4), turetimelist);			
		}
		
		ob.put("time", days);
		ob.put("legend", legend);		
		ob.put("truedatajson", truedatajson);
		ob.put("difdatajson", difdatajson);	
		ob.put("trueTimejson", trueTimejson);			
		return ob.toString();
	}
	
	/**
	 * 万年历淡旺季城市数据--折线图
	 * @param bus 
	 * @param city 
	 * @param cate
	 * @param dateGroup
	 * @param platform
	 * @param index
	 * @return
	 */
	
	public static String getSeasonCityLine(EnumCate bus,String dateGroup,String cate,String city,String platform,String index){
			
		//替换业务线的总体名称			
		String bus_sql="";
		if(bus.toString().equals("total")){
			bus_sql="all";
		}else
		{
			bus_sql=bus.toString();
		}
		
		//表后缀  二手创新和二手推广还在二手的表里。。
		String tableSuffix="";
		if(bus.toString().equals("total"))
			tableSuffix="all";
		else if(bus.toString().equals("ershouchuangxin") || bus.toString().equals("ershoutuiguang"))
		{
			tableSuffix="ershou";
		}else
		{
			tableSuffix=bus.toString();
		}
		
		String stat_type="";
		if(dateGroup.equals("stat_date"))
			stat_type="day";
		else if(dateGroup.equals("week_begin"))
			stat_type="week";
		else if(dateGroup.equals("month_num"))
			stat_type="month";

		//周的取年份时 用week_end
		String flagdata="";
		if(dateGroup.equals("stat_date"))
			flagdata="stat_date";
		else if(dateGroup.equals("week_begin"))
			flagdata="week_end";
		else if(dateGroup.equals("month_num"))
			flagdata="month_num";
		
		List<Object> days = Lists.newArrayList();
		JSONObject ob = new JSONObject();		
		
		String select = dateGroup+","+index+" as uv, substring("+flagdata+",1,4) as flag, holiday as festival";
		String condition = "business_type='"+bus_sql+"' AND disp_city1_name='"+city+"' AND disp_cate2_name='"+cate+"' AND stat_type='"+stat_type+"' AND platform='"+platform+"' group by "+dateGroup;			
		List<CalendarCity> list = calendarCityService.getListByConditon(tableSuffix,select, condition, dateGroup);
		//stat_dateMap对应数据   节日当天=[...]
		TreeMap<String, List<CalendarCity>> stat_dateMap = new TreeMap<String, List<CalendarCity>>(ReflectUtil.groupByField(list, "flag"));
		
		List<String> yearList = Lists.newArrayList();
	    HashMap<String,Integer> festivalMap = Maps.newHashMap();
	    for(String c:stat_dateMap.keySet())
	    {
	    	if(c.equals("2013"))
	    	{
	    		continue;
	    	}
	    	yearList.add(c);
	    	List<CalendarCity> curlist=stat_dateMap.get(c);
	    	int yearsum=0;
	    	int yeardays=0;
	    	for(Object vo: curlist)
	    	{
	    		int curdate_data= Integer.parseInt(ReflectUtil.getVal(vo, "uv").toString());
	    		yearsum+=curdate_data;
	    		yeardays++;
	    	}
	    	festivalMap.put(c, yearsum/yeardays);
	    }
	    
	    if(yearList.isEmpty())
		{
			return ob.toString();
		}
	    
		//分别获取各年对应的日期
	    JSONObject truedatajson = new JSONObject();   //真实值
		JSONObject festivaldatajson = new JSONObject();    //节日数据标记
		JSONObject trueTimejson = new JSONObject();   //对应的时间
		
		for(String fes:stat_dateMap.keySet())
		{
			if(fes.equals("2013"))
	    	{
	    		continue;
	    	}
		    List<Object> CountList = Lists.newArrayList();  //归一化数据
			List<Object> turedatalist=Lists.newArrayList();
			List<Object> festivaldatalist=Lists.newArrayList();
		    List<Object> turetimelist=Lists.newArrayList();
			
			List<CalendarCity> curList=stat_dateMap.get(fes);
			
		    for(Object vo: curList)
	    	{
		    	//展示的是所有数据除以节日当天
	    		double curdate_data= Integer.parseInt(ReflectUtil.getVal(vo, "uv").toString());
	    		double fes_data= festivalMap.get(fes);
	    		double t=(double)(Math.round(curdate_data/fes_data*10000)/10000.0);
				CountList.add(t);

				turedatalist.add(curdate_data);
				festivaldatalist.add(ReflectUtil.getVal(vo, "festival").toString());
		    	turetimelist.add(ReflectUtil.getVal(vo,dateGroup));   				
		    
		    	//去年的时间
		    	if(yearList.size()==1 || fes.equals(yearList.get(yearList.size()-2)))
		    	{
		    		days.add(ReflectUtil.getVal(vo, dateGroup));
		    	}
	    	}
		    
		    boolean flag=true;
		    //当年的数按照时间轴补齐 
	    	if(fes.equals(yearList.get(yearList.size()-1)))
    		{
	    		if(turedatalist.size()<days.size()){					
					while(turedatalist.size()<days.size())
					{
						festivaldatalist.add("");
						if(stat_type.equals("month"))
						{
							turetimelist.add(fes+days.get(turedatalist.size()).toString().substring(4,6));
						}
						else turetimelist.add(fes+"-"+days.get(turedatalist.size()).toString().substring(5,10));
										
						double avg=0;	
						int count=0;
						for(String s:truedatajson.keySet())
						{
							if(s.equals(yearList.get(yearList.size()-1)))
								continue;

							@SuppressWarnings("unchecked")
							List<Double> templist = (List<Double>)ob.get(s);
							//计算平均数时某年数缺
							if(templist.size()<=turedatalist.size())
							{
								avg+=templist.get(templist.size()-1);	
							}
							else avg+=templist.get(turedatalist.size());	
							count++;
						}
						if(flag)
						{
							JSONObject star = new JSONObject();  
							star.put("value", avg/count);
							star.put("symbol", "emptypin");
							star.put("symbolSize", 8);
							
							JSONObject itemStyle = new JSONObject(); 
							JSONObject normal = new JSONObject(); 
							normal.put("color", "red");
							itemStyle.put("normal", normal);
							star.put("itemStyle", itemStyle);
							
							CountList.add(star);
							flag=false;
						}
						else CountList.add(avg/count);						
						turedatalist.add("");
					}
				}
    		}
	    
			truedatajson.put(fes.substring(0, 4), turedatalist);
			festivaldatajson.put(fes.substring(0, 4), festivaldatalist);    	
		    ob.put(fes.substring(0, 4), CountList);		   
		    trueTimejson.put(fes, turetimelist);
		}  
		ob.put("time", days);
		ob.put("legend", yearList);	
		ob.put("truedatajson", truedatajson);
		ob.put("festivaldatajson", festivaldatajson);		
		ob.put("trueTimejson", trueTimejson);
		return ob.toString();
	}
	
	
	/**
	 * 万年历淡旺季来源数据--折线图
	 * @param bus 
	 * @param source 
	 * @param cate
	 * @param dateGroup
	 * @param platform
	 * @param index
	 * @return
	 */
	
	public static String getSeasonSourceLine(EnumCate bus,String dateGroup,String cate,String source,String platform,String index){			
		
		//替换业务线的总体名称
		String bus_sql="";
		if(bus.toString().equals("total"))
			bus_sql="all";
		else bus_sql=bus.toString();
	
		String stat_type="";
		if(dateGroup.equals("stat_date"))
			stat_type="day";
		else if(dateGroup.equals("week_begin"))
			stat_type="week";
		else if(dateGroup.equals("month_num"))
			stat_type="month";
		
		//周的取年份时 用week_end
		String flagdata="";
		if(dateGroup.equals("stat_date"))
			flagdata="stat_date";
		else if(dateGroup.equals("week_begin"))
			flagdata="week_end";
		else if(dateGroup.equals("month_num"))
			flagdata="month_num";

		List<Object> days = Lists.newArrayList();
		JSONObject ob = new JSONObject();		

		String select = dateGroup+","+index+" as uv, substring("+flagdata+",1,4) as flag, holiday as festival";
		String condition = "business_type='"+bus_sql+"' AND source1_name='"+source+"' AND disp_cate2_name='"+cate+"' AND stat_type='"+stat_type+"' group by "+dateGroup;			
		List<CalendarSource> list = calendarSourceService.getListByConditon(platform,select, condition, dateGroup);
	
		//stat_dateMap对应数据   节日当天=[...]
		TreeMap<String, List<CalendarSource>> stat_dateMap = new TreeMap<String, List<CalendarSource>>(ReflectUtil.groupByField(list, "flag"));
		    
	    List<String> yearList = Lists.newArrayList();
	    HashMap<String,Integer> festivalMap = Maps.newHashMap();
	    for(String c:stat_dateMap.keySet())
	    {
	    	if(c.equals("2013"))
	    	{
	    		continue;
	    	}
	    	yearList.add(c);
	    	List<CalendarSource> curlist=stat_dateMap.get(c);
	    	int yearsum=0;
	    	int yeardays=0;
	    	for(Object vo: curlist)
	    	{
	    		int curdate_data= Integer.parseInt(ReflectUtil.getVal(vo, "uv").toString());
	    		yearsum+=curdate_data;
	    		yeardays++;
	    	}
	    	festivalMap.put(c, yearsum/yeardays);
	    }
	    
	    if(yearList.isEmpty())
		{
			return ob.toString();
		}
	    
	    //分别获取各年对应的日期
	    JSONObject truedatajson = new JSONObject();   //真实值
		JSONObject festivaldatajson = new JSONObject();    //节日数据标记
		JSONObject trueTimejson = new JSONObject();   //对应的时间
	   
	    for(String fes:stat_dateMap.keySet())
	    {
	    	if(fes.equals("2013"))
	    	{
	    		continue;
	    	}
	    	List<Object> CountList = Lists.newArrayList();  //归一化数据
			List<Object> turedatalist=Lists.newArrayList();
			List<Object> festivaldatalist=Lists.newArrayList();
		    List<Object> turetimelist=Lists.newArrayList();
		    
		    List<CalendarSource> curlist=stat_dateMap.get(fes);
		    for(Object vo: curlist)
	    	{
		    	//展示的是所有数据除以节日当天
	    		double curdate_data= Integer.parseInt(ReflectUtil.getVal(vo, "uv").toString());
	    		double fes_data= festivalMap.get(fes);
	    		double t=(double)(Math.round(curdate_data/fes_data*10000)/10000.0);
				CountList.add(t); 
				
				turedatalist.add(curdate_data);
				festivaldatalist.add(ReflectUtil.getVal(vo, "festival").toString());
		    	turetimelist.add(ReflectUtil.getVal(vo,dateGroup));    
		    	
				//以去年的时间为横轴
		    	if(yearList.size()==1 || fes.equals(yearList.get(yearList.size()-2)))
		    	{
		    		days.add(ReflectUtil.getVal(vo, dateGroup));
		    	}
	    	}
		    
		    boolean flag=true;
		    //当年的数按照时间轴补齐 
	    	if(fes.equals(yearList.get(yearList.size()-1)))
    		{
	    		if(turedatalist.size()<days.size()){					
					while(turedatalist.size()<days.size())
					{
						festivaldatalist.add("");
						if(stat_type.equals("month"))
						{
							turetimelist.add(fes+days.get(turedatalist.size()).toString().substring(4,6));
						}
						else turetimelist.add(fes+"-"+days.get(turedatalist.size()).toString().substring(5,10));
										
						double avg=0;	
						int count=0;
						for(String s:truedatajson.keySet())
						{
							if(s.equals(yearList.get(yearList.size()-1)))
								continue;

							@SuppressWarnings("unchecked")
							List<Double> templist = (List<Double>)ob.get(s);
							//计算平均数时某年数缺
							if(templist.size()<=turedatalist.size())
							{
								avg+=templist.get(templist.size()-1);	
							}
							else avg+=templist.get(turedatalist.size());	
							count++;
						}
						if(flag)
						{
							JSONObject star = new JSONObject();  
							star.put("value", avg/count);
							star.put("symbol", "emptypin");
							star.put("symbolSize", 8);
							
							JSONObject itemStyle = new JSONObject(); 
							JSONObject normal = new JSONObject(); 
							normal.put("color", "red");
							itemStyle.put("normal", normal);
							star.put("itemStyle", itemStyle);
							
							CountList.add(star);
							flag=false;
						}
						else CountList.add(avg/count);						
						turedatalist.add("");
					}
				}
    		}
	    	
		    truedatajson.put(fes.substring(0, 4), turedatalist);
		    festivaldatajson.put(fes.substring(0, 4), festivaldatalist);    	
		    ob.put(fes.substring(0, 4), CountList);		
		    
		    trueTimejson.put(fes, turetimelist);
		}
	   
		ob.put("time", days);
		ob.put("legend", yearList);	
		ob.put("truedatajson", truedatajson);
		ob.put("festivaldatajson", festivaldatajson);		
		ob.put("trueTimejson", trueTimejson);	
		return ob.toString();
	}	
	
	
	/**
	 * 万年历淡旺季来源数据--漏斗图  月
	 * @param bus 
	 * @param source 
	 * @param season_Type
	 * @param month_num
	 * @param platform
	 * @return
	 */
	
	public static String getFunnelMonth(EnumCate bus,String source,String month_num,String season_Type,String platform){
			
		JSONObject ob = new JSONObject();		

		String select = "month_num,cate2_name,sum(info_valid_add_count) as info_valid_add_count ";
		//String condition = "business_name='"+bus.getDBName()+"' AND city='"+city+"' AND month_num='"+month_num+"'  and platform='"+platform+"' group by month_num";			
		
		String condition = "business_name='招聘' AND cate1_name='常规招聘' AND month_num='201510' group by cate2_name order by info_valid_add_count limit 10";			
		
		List<InfoCore> list = infoService.getListByConditon(select, condition, "");
		List<Object> legend = Lists.newArrayList();
		List<Object> datalist = Lists.newArrayList();		
				
	    for (InfoCore vo : list) {
	    
			JSONObject data = new JSONObject();			
			data.put("name",vo.getCate2_name());
			data.put("value", vo.getInfo_valid_add_count()/1500);
			datalist.add(data);
			legend.add(vo.getCate2_name());
		} 		
	    
		ob.put("datalist", datalist);
		ob.put("legend", legend);
		
		JSONObject comment = new JSONObject();
		comment.put("美容/美发", "high");
		comment.put("教育培训", "low");
		
		ob.put("comment", comment);
		return ob.toString();
	}	
	
	/**
	 * 万年历淡旺季来源数据--漏斗图  周
	 * @param bus 
	 * @param source 
	 * @param season_Type
	 * @param week_day
	 * @param platform
	 * @return
	 */
	
	public static String getFunnelWeek(EnumCate bus,String source,String week_day,String season_Type,String platform){
			
		JSONObject ob = new JSONObject();		
		
		week_day="2015-10-01";
		//先换算出本周的周一日期	
		String sel =" subdate('"+week_day+"',date_format('"+week_day+"','%w')-1) as stat_date";
		String con = " ";		
		List<InfoCore> weekdata = infoService.getListByConditon(sel, con, "stat_date");
		
		String week_begin=weekdata.get(0).getStat_date();
		
		String select = "week_begin,cate2_name,sum(info_valid_add_count) as info_valid_add_count ";
		//String condition = "business_name='"+bus.getDBName()+"' AND city='"+city+"' AND month_num='"+month_num+"'  and platform='"+platform+"' group by month_num";			
		
		String condition = "business_name='招聘' AND cate1_name='常规招聘' AND week_begin='"+week_begin+"' group by cate2_name order by info_valid_add_count limit 10";			
		
		List<InfoCore> list = infoService.getListByConditon(select, condition, "");
		List<Object> legend = Lists.newArrayList();
		List<Object> datalist = Lists.newArrayList();		
				
	    for (InfoCore vo : list) {
	    
			JSONObject data = new JSONObject();			
			data.put("name",vo.getCate2_name());
			data.put("value", vo.getInfo_valid_add_count()/300);
			datalist.add(data);
			legend.add(vo.getCate2_name());
		} 		
	    
		ob.put("datalist", datalist);
		ob.put("legend", legend);
		
		JSONObject comment = new JSONObject();
		comment.put("美容/美发", "high");
		comment.put("教育培训", "low");
		
		ob.put("comment", comment);
		return ob.toString();
	}	
}
